<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card shadow="hover">
      <!-- 搜索 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getUserList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUserList()"
            ></el-button></el-input
        ></el-col>
        <el-col :span="4">
          <el-button type="primary" @click="adduservisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <el-table stripe style="width: 100%" :data="userlist">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <!-- 状态 -->
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="changeUserState(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 修改信息 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" @click="userDelete(scope.row.id)"></el-button>
            <el-tooltip
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button type="warning" icon="el-icon-setting"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total,prev, pager, next"
        :total="this.total"
        @current-change="handleCurrentChange"
        :page-size="this.queryInfo.pagesize"
        :current-page="this.queryInfo.pagenum"
      >
      </el-pagination>
    </el-card>
    <!-- 添加用户对话框 -->
    <el-dialog title="添加" :visible.sync="adduservisible" width="30%">
      <el-form
        ref="form"
        :model="addinfo"
        label-width="70px"
        :rules="addUserRules"
      >
        <el-form-item label="用户名：" prop="name">
          <el-input v-model="addinfo.username"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="addinfo.password"></el-input>
        </el-form-item>
        <el-form-item label="e-mail：">
          <el-input v-model="addinfo.email"></el-input>
        </el-form-item>
        <el-form-item label="电话：">
          <el-input v-model="addinfo.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adduservisible = false">取 消</el-button>
        <el-button type="primary" @click="adduser()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改信息对话框 -->
    <el-dialog
      title="修改信息"
      :visible.sync="editDialogVisible"
      width="70%"
      @close="editDialogClose"
    >
      <el-form ref="form1" :model="editUserInfo" label-width="70px">
        <el-form-item label="用户名：">
          <el-input v-model="editUserInfo.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="e-mail：">
          <el-input v-model="editUserInfo.email"></el-input>
        </el-form-item>
        <el-form-item label="电话：">
          <el-input v-model="editUserInfo.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2,
      },
      userlist: [],
      total: 0,
      adduservisible: false,
      editDialogVisible: false,
      addinfo: {
        username: '',
        password: '',
        email: '',
        mobile: '',
      },
      addUserRules: {
        name: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
        ],
      },
      editUserInfo: {},
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$axios.get('users', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) return this.$messege.error(res.meta.msg)
      this.userlist = res.data.users
      this.total = res.data.total
      //console.log(res)
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getUserList()
    },
    async changeUserState(userinfo) {
      const { data: res } = await this.$axios.put(
        `users/${userinfo.id}/state/${userinfo.mg_state}`
      )
      this.$message.success('成功！')
    },
    async adduser() {
      this.adduservisible = false
      const { data: res } = await this.$axios.post('users', this.addinfo)
      console.log(res)
      if (res.meta.status !== 201) return this.$message.error('添加失败！')
      this.$message.success('添加成功！')
      this.getUserList()
    },
    async showEditDialog(id) {
      this.editDialogVisible = true
      const { data: res } = await this.$axios.get('users/' + id)
      console.log(res.data)
      this.editUserInfo = res.data
    },
    editDialogClose() {
      this.$refs.form1.resetFields()
    },
    async editDialogInfo() {
      const { data: res } = await this.$axios.put(
        'users/' + this.editUserInfo.id,
        { email: this.editUserInfo.email, mobile: this.editUserInfo.mobile }
      )
      console.log(res)
      this.editDialogVisible=false
      this.getUserList()
      this.$message.success('修改成功！')
    },
    async userDelete(id){
      const {data:res}=await this.$axios.delete('users/'+id)
      console.log(res.mate)
      if(res.meta.status!==200) return this.$message.error('删除失败！')
      this.getUserList()
      this.$message.success('删除成功！')
    }
  },
}
</script>

<style lang="scss" scoped>
</style>